/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.onurkagan.ksnack.slice;

import com.onurkagan.ksnack.ResourceTable;
import com.onurkagan.ksnack_lib.Animations.Fade;
import com.onurkagan.ksnack_lib.MinimalKSnack.MinimalKSnack;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;

/**
 * BlankFraction
 *
 * @since 2021-07-08
 */
public class BlankFraction extends Fraction {
    private Component parse;
    private Button btnShow;
    private Button btnDismiss;
    private ComponentContainer blank;
    private MinimalKSnack minimalKsnack;
    private DependentLayout holder;
    private Text message;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        parse = scatter.parse(ResourceTable.Layout_blank, container, false);
        initcomponent();
        return parse;
    }

    private void initcomponent() {
        blank = (ComponentContainer) parse.findComponentById(ResourceTable.Id_blank);
        minimalKsnack = new MinimalKSnack(blank);
        btnShow = (Button) parse.findComponentById(ResourceTable.Id_fraction_main_btn_show);
        btnDismiss = (Button) parse.findComponentById(ResourceTable.Id_fraction_main_btn_dissmis);
        holder = (DependentLayout) parse.findComponentById(ResourceTable.Id_blank_snack_bar_rlv_holder);
        message = (Text) parse.findComponentById(ResourceTable.Id_blank_snack_bar_txt_message);
        btnShow.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                message.setText("This is minimal KSnack on Fragment !");
                holder.setVisibility(Component.VISIBLE);
                minimalKsnack
                    .setAnimations(Fade.In.getAnimations(holder), Fade.Out.getAnimations(holder))
                    .shows();
            }
        });

        btnDismiss.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                minimalKsnack.dismisss();
            }
        });
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }
}
